package com.sh.myPredicateFactory;
import lombok.Data;
import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import java.time.LocalTime;
import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;

@Component
public class ServiceTimeBetweenRoutePredicateFactory extends AbstractRoutePredicateFactory<ServiceTimeBetweenRoutePredicateFactory.Config> {
    public ServiceTimeBetweenRoutePredicateFactory() {
        super(Config.class);
    }

    // 逻辑业务处理
    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        LocalTime startTime = config.getStartTime();
        LocalTime endTime = config.getEndTime();
        Predicate<ServerWebExchange> predicate = new Predicate<>() {
            public boolean test(ServerWebExchange exchange) {
                LocalTime now = LocalTime.now();
                return now.isAfter(startTime) && now.isBefore(endTime);
            }
        };

        return predicate;
    }

    @Override
    public List<String> shortcutFieldOrder(){
        return Arrays.asList("startTime","endTime");
    }

    /**
     * 配置类
     */
    @Data
    public static class Config {
        private LocalTime startTime;
        private LocalTime endTime;
    }


}
